package com.usabb.domain;


import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.ifast.common.base.BaseDO;
import com.usabb.pojo.vo.CombosAddVO;
import com.usabb.pojo.vo.CombosCategoryAddVO;
import com.usabb.utils.ListUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 月子中心 套餐 . 服务类目
 * </pre>
 * <small> 2019-08-19 18:03:27 | Aron</small>
 */
@Slf4j
@Data
@SuppressWarnings("serial")
@TableName("combos_category")
@EqualsAndHashCode(callSuper=true) 
public class CombosCategoryDO extends BaseDO {
	@TableId
	private Long id; /** 服务类目Id */

	private Long baseId;

    /** 外键,套餐Id */
    @TableField
    private Long combosId;

    /** 服务类目,如: 安心服务保障 */
    @TableField
    private String category;

    @TableField(exist = false)
    private List<CombosCategoryItemDO> items;




    public static CombosCategoryDO of(String category) {
        CombosCategoryDO categoryDO = new CombosCategoryDO();
        categoryDO.setCategory(category);

        return categoryDO;
    }
    public static List<CombosCategoryDO> of(List<String> categoryNames) {
        return categoryNames.stream()
                .map(it -> CombosCategoryDO.of(it))
                .collect(Collectors.toList());
    }

    public static List<CombosCategoryDO> from(CombosCategoryAddVO categoryAddVO) {
        List<CombosCategoryDO> categoryDOS = from(categoryAddVO.getCategory(), categoryAddVO.getCategoryTrCount(),
                categoryAddVO.getItemNo(), categoryAddVO.getItemName(),
                categoryAddVO.getItemRemark(), categoryAddVO.getItemIntro());

        categoryDOS.forEach(it -> it.setBaseId(categoryAddVO.getBaseId()));

        return categoryDOS;
    }


    public static List<CombosCategoryDO> from(CombosAddVO addVO) {
        return from(addVO.getCategory(), addVO.getCategoryTrCount(), addVO.getItemNo(), addVO.getItemName(),
                addVO.getItemRemark(), addVO.getItemIntro());
    }

    public static List<CombosCategoryDO> from(List<String> categoryNames, String categoryTrCount,
                                              List<String> itemNo, List<String> itemName, List<String> itemRemark,
                                              List<String> itemIntro) {

        final String[] categoryItemCounts = StringUtils.split(categoryTrCount, CombosAddVO.DELIMITER);  //每个类目下个item个数
        if (categoryNames.size() != categoryItemCounts.length) {
            log.error("Input something wrong. categoryNames.size: {}, itemCounts: {}",
                    categoryNames.size(), categoryItemCounts.length);
            return null;
        }

        final List<Integer> trCountList = Arrays.stream(categoryItemCounts)
                .map(it -> Integer.valueOf(it))
                .collect(Collectors.toList());

        final List<List<String>> itemNoSlices = ListUtils.slice(itemNo, trCountList);
        final List<List<String>> itemNameSlices = ListUtils.slice(itemName, trCountList);
        final List<List<String>> itemRemarkSlices = ListUtils.slice(itemRemark, trCountList);
        final List<List<String>> itemIntroSlices = ListUtils.slice(itemIntro, trCountList);

        List<CombosCategoryDO> categories = CombosCategoryDO.of(categoryNames);
        for (int i=0; i<categories.size(); i++) {
            final CombosCategoryDO category = categories.get(i);

            final List<String> itemNoList = itemNoSlices.get(i);
            final List<String> itemNameList = itemNameSlices.get(i);
            final List<String> itemRemarkList = itemRemarkSlices.get(i);
            final List<String> itemIntroList = itemIntroSlices.get(i);

            List<CombosCategoryItemDO> itemList = CombosCategoryItemDO
                    .ofItems(itemNoList, itemNameList, itemRemarkList, itemIntroList);
            category.setItems(itemList);
        }

        return categories;
    }

}
